package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.entity.GroupInformation;
import com.lin.entity.ProjectDetail;
import com.lin.entity.ProjectStatus;
import com.lin.entity.Vote;
import com.lin.mapper.*;
import com.lin.security.util.SecurityUtil;
import com.lin.service.VoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.util.CountMemberNumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2022-04-18
 */
@Service
public class VoteServiceImpl extends ServiceImpl<VoteMapper, Vote> implements VoteService {

    @Resource
    GroupInformationMapper groupInformationMapper;

    @Resource
    ProjectDetailMapper projectDetailMapper;

    @Resource
    ProjectStatusMapper projectStatusMapper;

    @Resource
    ProjectOutsideMapper projectOutsideMapper;

    @Override
    @Transactional
    public void vote(Vote vote) {
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        ProjectDetail projectDetail = projectDetailMapper.selectById(vote.getProjectId());
        Integer groupId = projectDetail.getGroupId();
        GroupInformation groupInformation = groupInformationMapper.selectOne(new QueryWrapper<GroupInformation>()
                .eq("group_id", groupId));
        //判断项目是否开启了投票
        ProjectStatus projectStatus = projectStatusMapper.selectById(vote.getProjectId());
        if (projectStatus == null) {
            throw new RuntimeException("该项目还没发起投票");

        }
        //投票的前提是本组成员
        Integer masterId = groupInformation.getMasterId();
        Integer memberOneId = groupInformation.getMemberOneId();
        Integer memberTwoId = groupInformation.getMemberTwoId();
        Integer memberThreeId = groupInformation.getMemberThreeId();
        Integer memberFourId = groupInformation.getMemberFourId();
        if ((masterId.equals(currentUserUid)) || (memberOneId != null && memberOneId.equals(currentUserUid))
                || (memberTwoId != null && memberTwoId.equals(currentUserUid))
                || (memberThreeId != null && memberThreeId.equals(currentUserUid))
                || (memberFourId != null) && memberFourId.equals(currentUserUid)) {
            //还要判断是否重复投票
            if (this.getOne(new QueryWrapper<Vote>()
                    .eq("group_id", groupId)
                    .eq("user_id", currentUserUid)) != null) {
                throw new RuntimeException("您已经投过票,请勿重复投票！");
            }
            //投票
            vote.setGroupId(groupId);
            vote.setUserId(currentUserUid);
            this.save(vote);
        } else {
            throw new RuntimeException("很抱歉,您不是团队成员,无法进行投票！");
        }
        //计算同意率
        double memberNumber = CountMemberNumberUtils.countMemberNumber(groupInformation);
        Double state = 0.0;//同意百分比
        int agreeMember = this.count(new QueryWrapper<Vote>().eq("group_id", groupId)
                .eq("type", 0));
        state = agreeMember / memberNumber;
        projectStatus.setState(state);
        projectStatusMapper.updateById(projectStatus);
        ProjectStatus projectStatusDB = projectStatusMapper.selectById(projectStatus);
        if (projectStatusDB.getState() >= 0.5) { //同意占比超过半数
            Integer projectId = projectStatusDB.getProjectId();
            Integer groupId1 = projectDetailMapper.selectById(projectId).getGroupId();
            projectDetailMapper.deleteById(projectId);
            projectOutsideMapper.deleteById(projectId);
            groupInformationMapper.deleteById(groupId1);
            projectStatusMapper.delete(new QueryWrapper<ProjectStatus>().eq("project_id", projectId));
        }
    }
}
